import axios from "axios";
import ELMessage from "element-plus";

const service = axios.create({
    baseURL:  import.meta.env.VITE_baseUrl,
    headers:{
        'Content-type':' application/json;charset=UTF-8'
    },
    timeout:5000
})

service.interceptors.request.use((config)=>{
    config.headers.common['Autherization'] = window.localStorage.getItem('token');
    return config
})

service.interceptors.response.use((response)=>{

    const res = response.data

    if (import.meta.env.DEV) {
        console.info('接口数据',res)
    }

    switch (res.code) {
        case 200:
            return res;
        case 300:
            ELMessage.warning(res.msg || '认证识别')
            break;
        case 302:
            ELMessage.error(res.msg || '网络重定向')
            break;
        case 400:
            ELMessage.error(res.msg || '未知错误')
            break;
        case 500:
            ELMessage.error(res.msg || '网络异常')
            break;
        default:
            return false;
    }
})

export default service
